package com.redoor.app.stake.workflow.timetask;

import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.workflow.model.BaseWorkorderOperationLog;
import com.redoor.app.stake.workflow.service.BaseWorkorderOperationLogService;
import com.redoor.app.stake.workflow.service.WorkOrderLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

@Configuration
@EnableScheduling
public class WorkOrderLogSchedulingConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    BaseWorkorderOperationLogService operationLogService;

    @Autowired
    private WorkOrderLogService logService;

    /**  
     *  定时把redis里面的工单记录保存到数据库
     *   
     * @author ZhangMumu  
     * @date 2018/4/17 0017 21:51  
     * @param
     * @return
     */  
    @Scheduled(fixedRate = 10000)
    public void dealWorkOrderLogIntoDataBase() {
        logger.info("dealWorkOrderLogIntoDataBase begin");
        try {
            List<BaseWorkorderOperationLog> logList = logService.getWorkOrderLogFromRedis(10, LogEnum.WORKORDER_LIFE_LOG);
            if(logList!=null && logList.size() > 0){
                try{
                    if (0 < operationLogService.saveAll(logList)){
                        logger.info("WorkOrderLogSchedulingConfig_dealWorkOrderLogIntoDataBase_insertWorkOrderLogSuccess");
                    }
                }catch (Exception e){
                    logger.error("WorkOrderLogSchedulingConfig_dealWorkOrderLogIntoDataBase_insertWorkOrderLogFailed:" + e.getLocalizedMessage());
                }
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        logger.info("dealWorkOrderLogIntoDataBase end");
    }

}
